package com.zone.binding;

import com.zone.session.SqlSession;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * 所有mapper接口对应的代理处理类
 *
 * @author: zongzi
 * @description: TODO
 * @date: 2023/11/26
 */
public class MapperProxy<T> implements InvocationHandler {
    /**
     * 代理接口
     */
    private final Class<T> mapperInterface;
    /**
     * 模拟保存所有sql的集合
     * key:接口全限定类名.接口方法名，value：sql
     */
    private final SqlSession sqlSession;

    public MapperProxy(Class<T> mapperInterface, SqlSession sqlSession) {
        this.mapperInterface = mapperInterface;
        this.sqlSession = sqlSession;
    }


    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        // 如果代理的方法是Object的方法则不进行代理
        if (Object.class.equals(method.getDeclaringClass())) {
            return method.invoke(proxy, args);
        } else {
            // 接口全限定类名.方法名称
            String key = String.format("%s.%s", mapperInterface.getName(), method.getName());
            return "代理" + key;
        }
    }
}
